package org.rsbot.script.wrappers;

import org.rsbot.client.Client;
import org.rsbot.client.Model;
import org.rsbot.client.RSGround;
import org.rsbot.client.RSGroundEntity;
import org.rsbot.client.RSGroundObject;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.MethodProvider;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Tiles;

public class RSGroundItem extends MethodProvider
{
  private RSItem groundItem;
  private RSTile location;

  public RSGroundItem(MethodContext paramMethodContext, RSTile paramRSTile, RSItem paramRSItem)
  {
    super(paramMethodContext);
    this.location = paramRSTile;
    this.groundItem = paramRSItem;
  }

  public RSModel getModel()
  {
    int i = this.location.getX() - this.methods.game.getBaseX();
    int j = this.location.getY() - this.methods.game.getBaseY();
    int k = this.methods.client.getPlane();
    RSGround localRSGround = this.methods.client.getRSGroundArray()[k][i][j];

    if (localRSGround != null) {
      RSGroundEntity localRSGroundEntity = localRSGround.getGroundObject();
      if (localRSGroundEntity != null) {
        Model localModel = ((RSGroundObject)localRSGround.getGroundObject()).getModel();
        if (localModel != null) {
          return new RSAnimableModel(this.methods, localModel, localRSGroundEntity);
        }
      }
    }
    return null;
  }

  public boolean doAction(String paramString) {
    RSModel localRSModel = getModel();
    if (localRSModel != null) {
      this.methods.mouse.move(localRSModel.getPoint());
      sleep(random(5, 50));
      return this.methods.menu.doAction(paramString);
    }
    return this.methods.tiles.doAction(getLocation(), random(0.45D, 0.55D), random(0.45D, 0.55D), 0, paramString);
  }

  public RSItem getItem() {
    return this.groundItem;
  }

  public RSTile getLocation() {
    return this.location;
  }

  public boolean isOnScreen() {
    RSModel localRSModel = getModel();
    if (localRSModel == null) {
      return this.methods.calc.tileOnScreen(this.location);
    }
    return this.methods.calc.pointOnScreen(localRSModel.getPoint());
  }
}